<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class Email extends M_Controller
{
    public function sendEmail()
    {
        $config = require __DIR__ . '/../config/smtp.php';
        /*header("content-type:text/html;charset=utf-8");*/
//        ini_set("magic_quotes_runtime", 0);
        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            if (empty($_GET['oid'])) {
                throw new Exception('参数缺失');
            }
            $orderInfo = $this->models('module/order')->orderInfo(
                $_GET['oid'],
                'id,buy_username,apply_invoice,certificate',
                $this->uid
            );
            if (empty($orderInfo)) {
                throw new Exception('订单信息不存在');
            }
            $email = string2array($orderInfo['apply_invoice'])['email'];
            if (empty($email)) {
                throw new Exception('客户暂未上传邮箱地址');
            }
            //Server settings
            $mail->SMTPDebug = 0;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = $config['host'];                        // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->CharSet='UTF-8';
            //smtp登录的账号
            $mail->Username = $config['username'];                // SMTP username
            //smtp登录的密码
            $mail->Password = $config['password'];                // SMTP password
            //设置使用ssl加密方式登录鉴权
            $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 465;                                    // TCP port to connect to
            //Recipients
            $mail->setFrom($config['username'], $config['sign']);
            //设置收件人邮箱地址 该方法有两个参数 第一个参数为收件人邮箱地址
            // 第二参数为给该地址设置的昵称 不同的邮箱系统会自动进行处理变动 这里第二个参数的意义不大
            $mail->addAddress($email, $orderInfo['buy_username']);         // Add a recipient
            //$mail->addAddress('ellen@example.com');              // Name is optional
            //答复
            $mail->addReplyTo($email, $orderInfo['buy_username']);
            // 抄送地址
            //$mail->addCC('cc@example.com');
            //$mail->addBCC('bcc@example.com');

            //Attachments 附件
            $mail->addAttachment(
                WEBPATH . 'public/uploadfile/' .
                get_attachment($orderInfo['certificate'])['attachment'],
                '发票信息'
            );
            //Content
            $mail->isHTML(true);                                    // Set email format to HTML
            $mail->Subject = $config['subject'];
            $mail->Body = $config['body'] . '<b>---' . $config['sign'] . '</b>';
            $mail->AltBody = $config['altBody'];
            $mail->send();
            x_json(1, '已成功发送邮件');
        } catch (Exception $e) {
            x_json(0, $e->getMessage() . '#' . $mail->ErrorInfo);
        }
    }

}
